Ext.define('Packt.controller.Login',{ 
				
		    extend: 'Ext.app.Controller',
		
		     views: [
			        	'Login' ,
			        	//'Header',
			        	'authentication.CapsLockTooltip'
			        	
			        	
			        ],
			        
			refs: [
						{
						ref: 'capslockTooltip',
						selector: 'capslocktooltip'
						}
				  ],
			 
		requires: [
		           		'Packt.util.MD5'
		          ], 
			        
			     	      
			init: function(application) 
			{ 
				this.control({
					
					
					"button#submit": { 
											click: this.onButtonClickSubmit 
									 },
												
					"button#cancel": { 
											click: this.onButtonClickCancel 
									 },
					
				    "login form textfield": {
										 		specialkey: this. onTextfieldSpecialKey
				                            },
				                            
				  "login form textfield[name=password]": {
				                            	keypress: this.onTextfieldKeyPress
				                            	},
				   "appheader button#logout": {
				                            	click: this.onButtonClickLogout
				                               }
				});
			 },
			 	 
			//------------------------ implementation of methods to be executed when event happens------------
				 
			onButtonClickSubmit: function( button, e, options)
			{
				
				
				
				//Packt.util.SessionMonitor.start();
				var formPanel = button.up('form'),login = button.up('login'),
				user = formPanel.down('textfield[name=user]').getValue(),
				pass = formPanel.down('textfield[name=password]').getValue();
				
				if (formPanel.getForm().isValid())
				{
				
					
					// encrypting password
			      pass = Packt.util.MD5.encode(pass);
					
			       Ext.get(login.getEl()).mask("Authenticating... Please wait...",	'loading');
			   
			      // sending user details to server
					Ext.Ajax.request({
										url: 'http://localhost:8080/masteringextjs/rest/json/user/login',
										params: {
													user: user,
													password: pass,
												},
									
							
							success: function(conn, response, options, eOpts) 
							{
       								Ext.get(login.getEl()).unmask();
									var result = Ext.JSON.decode(conn.responseText, true);
									if (!result)
									{ 
											result = {};
											result.success = false;
											result.msg = conn.responseText;
									}
									if (result.success) 
									{
											login.close(); 
											Ext.create('Packt.view.MyViewport'); 
									 } 
									else 
									{
											Ext.Msg.show({
													
																title:'Fail!',
																msg: result.msg, 
																icon: Ext.Msg.ERROR,
																buttons: Ext.Msg.OK
														});
									  }
								},
												
								// failure function
					
								failure: function(conn, response, options, eOpts)
								{
									
									
										Ext.get(login.getEl()).unmask();
										Ext.Msg.show({
														title:'Error!',
														msg: conn.responseText,
														icon: Ext.Msg.ERROR,
														buttons: Ext.Msg.OK
													});
								}
					    });
					
					
				} 
				
						console.log('login submit');
					
			},

			onButtonClickCancel: function(button, e, options)
			{
			     
					button.up('window').down('form').getForm().reset();
					
			},
			
			onTextfieldSpecialKey: function(field, e, options)
			{
					if (e.getKey() == e.ENTER)
					{
						var submitBtn = field.up('form').down('button#submit');
						submitBtn.fireEvent('click', submitBtn, e, options);
					}
			},
			
			onTextfieldKeyPress: function(field, e, options) 
			{
					var charCode = e.getCharCode(); 
					if((e.shiftKey && charCode >= 97 && charCode <= 122) || (!e.shiftKey && charCode >= 65 && charCode <= 90))
					{
						if(this.getCapslockTooltip() === undefined)
						{ 
							Ext.widget('capslocktooltip'); 
						}
							this.getCapslockTooltip().show();
					} 
					else
					{
						if(this.getCapslockTooltip() !== undefined)
						{ 
							this.getCapslockTooltip().hide(); 
						}
					}
			 },
			
			onButtonClickLogout: function(button, e, options) 
			{
				Ext.Ajax.request
				({
					
					url: 'php/logout.php',
					success: function(conn, response, options, eOpts)
					{
						var result = Packt.util.Util.decodeJSON(conn.responseText);
						if (result.success)
						{
							button.up('mainviewport').destroy();
							window.location.reload();
						}
						else
						{
							Packt.util.Util.showErrorMsg(conn.responseText);
						}
					},
					
					failure: function(conn, response, options, eOpts)
					{
						Packt.util.Util.showErrorMsg(conn.responseText);
					}
				});
			}
	});
				
				